package com.example.wtt.service;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.wtt.common.AES;
import com.example.wtt.entity.Validation;
import com.example.wtt.mapper.ValidationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ValidationService extends ServiceImpl<ValidationMapper, Validation> {
    @Autowired
    private ValidationMapper validationMapper;
    private AES aes;

    @Transactional
    public void saveEmailCode(String email, String emailCode, DateTime offsetMinute, int type) throws Exception {
        Validation validation = new Validation();
        validation.setEmail(email);
        validation.setCode(emailCode);
        validation.setTime(offsetMinute);
        validation.setType(type);

        UpdateWrapper<Validation> validationUpdateWrapper = new UpdateWrapper<>();
        validationUpdateWrapper.eq("email",aes.encrypt(email.getBytes()));
        remove(validationUpdateWrapper);
        save(validation);
    }
}
